package chpt12;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class StatementTest2 {
	//本示例建立一个Connection。在这个Connection上执行了多条SQL语句
	public static void main(String[] args) {

		ResultSet rs = null;
		String url = "jdbc:mysql://localhost:3306/demodb?serverTimezone=Asia/Chongqing";
		String userName = "root";
		String password = "root123";
		System.out.println("请输入SQL语句：");
		//Scanner sc = new Scanner(System.in);
		//可使用如下两个SQL语句测试：
		//查询：select * from students
		//更新：update students set gpa = gpa where gender = 'm'
		// String sql = "update students set gpa = gpa+0.01 where id>3";
		String sql = "select * from students";
		try (Connection con = DriverManager.getConnection(url, userName, password);
				Statement stmt = con.createStatement();) {
			boolean result = stmt.execute(sql);
			if (result) {
				rs = stmt.getResultSet();
				rs.next();
				System.out.println(rs.getInt(1));
				System.out.println(sql+" 是查询类语句");
			}else {
				int updateCount = stmt.getUpdateCount();
				System.out.println(sql+"是更新类语句");
				System.out.println("影响行数:"+updateCount);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		//sc.close();
	}
}

